package com.atguigu.huffmanTree;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class HuffmanTree {
    public static void main(String[] args) {
          int  []  arr={1,5,45,6,3,65,24};
          createHuffmanTree(arr);
    }
    public  static   void    createHuffmanTree(int  [] arr){
       List<Node>  nodes = new ArrayList<Node>();
       for (int  value: arr){
           nodes.add(new Node(value));
       }
        Collections.sort(nodes);
        System.out.println("Node"+nodes);
        Node  leftNode=nodes.get(0);
        Node  rightNode=nodes.get(1);
        Node  parent=new Node(leftNode.value+rightNode.value);
        parent.left=leftNode;
        parent.right=rightNode;
        nodes.remove(leftNode);
        nodes.remove(rightNode);
        nodes.add(parent);
        Collections.sort(nodes);
        System.out.println("Nodes"+nodes);

    }
}

class  Node  implements Comparable<Node>{
    int  value;
    Node  left;
    Node    right;
    public Node(int  value){
        this.value=value;
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

    @Override
    public int compareTo(Node o) {
        //从小到大排序
        return this.value-o.value;
    }
}
